package com.dxtx.util;

import android.util.Log;


/**
 * Created by t00284576 on 2015/10/27.
 */
public class DLog {
    static String className;
    static String methodName;
    static int lineNumber;

    /**
     * 日志控制：发布版本不�?要打印日�? 当CURRENT = VERBOSE时所有日志都打印 当CURRENT = NOTHING�?
     * �?有日志都不打�? zwx220990
     */
    private static int VERBOSE = 0;
    private static int DEBUG = 1;
    private static int INFO = 2;
    private static int WARNING = 3;
    private static int ERROR = 4;
    private static int NOTHING = 5;
    private static int CURRENT = isDebuggable() ? VERBOSE : WARNING;

    public static boolean isDebuggable() {
        return true;
    }

    private static String createLog(String log) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        buffer.append(className);
        buffer.delete(buffer.length() - 4, buffer.length());
        buffer.append(methodName);
        buffer.append(":");
        buffer.append(lineNumber);
        buffer.append("]");
        buffer.append(log);

        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    public static void e(String tag, String message) {

        if (ERROR >= CURRENT) {
            if (!isDebuggable()) {
                Log.e(tag, message + "");
                return;
            }
            getMethodNames(new Throwable().getStackTrace());
            Log.e(tag, createLog(message));
        }
    }

    public static void i(String tag, String message) {
        if (INFO >= CURRENT) {
            if (!isDebuggable()) {
                Log.i(tag, message + "");
                return;
            }
            getMethodNames(new Throwable().getStackTrace());
            Log.i(tag, createLog(message));
        }

    }

    public static void d(String tag, String message) {
        if (DEBUG >= CURRENT) {
            if (!isDebuggable()) {
                Log.d(tag, message + "");
                return;
            }
            getMethodNames(new Throwable().getStackTrace());
            Log.d(tag, createLog(message));
        }
    }

    public static void v(String tag, String message) {
        if (VERBOSE >= CURRENT) {
            if (!isDebuggable()) {
                Log.v(tag, message + "");
                return;
            }

            getMethodNames(new Throwable().getStackTrace());
            Log.v(tag, createLog(message));
        }
    }

    public static void w(String tag, String message) {
        if (WARNING >= CURRENT) {
            if (!isDebuggable()) {
                Log.w(tag, message + "");
                return;
            }

            getMethodNames(new Throwable().getStackTrace());
            Log.w(tag, createLog(message));
        }
    }

    public static void e(Exception e) {
        Log.e("px", e.toString());
    }
}
